<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="views/include :: header('API列表')"/>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div style="padding-bottom: 10px;" id="LAY_lay_add">
            <button class="layui-btn layuiadmin-btn-role " data-type="add" id="add">
                <i class="layui-icon layui-icon-add-circle-fine"></i> 添加API
            </button>
            <div class="layui-input-inline" style="width: 200px;">
                <input type="text" name="search" id="tree" lay-filter="tree" placeholder="请输入分类名称" autocomplete="off"
                       class="layui-input">
            </div>
            <div class="layui-input-inline" style="width: 200px;">
                <input type="text" name="title" id="title" placeholder="请输入API名称" autocomplete="off"
                       class="layui-input">
            </div>
            <div class="layui-input-inline" style="width: 100px;">
                <button type="button" class="layui-btn layui-btn-normal" id="btn-search">
                    <i class="layui-icon layui-icon-search"></i> 搜索
                </button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn  layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑/预览</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="export"><i class="layui-icon">&#xe67d;</i>导出到本地</a>
    <a class="layui-btn layui-btn-danger  layui-btn-sm" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
</script>

<script type="text/javascript">

    layui.use(['form','table','layer', 'treeSelect'], function () {
        const form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            table = layui.table,
            treeSelect = layui.treeSelect;

        $("#btn-search").click(function () {
            let categoryId = $("#tree").val();
            let title = $("#title").val();
            table.reload('munu-table', {
                where: {"categoryId": categoryId,"title":title},
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        $("#add").click(function () {
            window.location.href = '/apidetail/toApiAdd';
        });

        treeSelect.render({
            // 选择器
            elem: '#tree',
            // 数据
            data: '/apidetail/getTreeSelectVos',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '请选择所属目录',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function (d) {
                 $("#tree").val(d.current.id);
                table.reload('munu-table', {
                    where: {"categoryId": d.current.id},
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            },
            // 加载完成后的回调函数
            success: function (d) {
//                选中节点，根据id筛选
//                treeSelect.checkNode('tree', 3);
//                获取zTree对象，可以调用zTree方法
//                var treeObj = treeSelect.zTree('tree');
//                console.log(treeObj);
//                刷新树结构
//                treeSelect.refresh();
            }
        });

        let tab_tab = table.render({
            elem: '#munu-table',
            url: '/apidetail/listApi', //数据接口
            page: true,
            limit: 10,
            limits: [10, 20, 30],
            cols: [
                [
                    {title: "分类名称", field: "categoryName",width:100, align: "center"},
                    {title: "API名称", field: "title", align: "center"},
                    {title: "最新版本", field: "version", align: "center",
                        templet: function (d) {
                            if (d.version != null) {
                                return 'V '+d.version+'.0';
                            }
                            return '';
                        }
                    },
                    {title: "发布人", field: "createUser", align: "center"},
                    {
                        title: "发布时间", field: "createTime",width: 180,  align: "center",
                        templet: function (d) {
                            if (d.createTime != null) {
                                return layui.util.toDateString(d.createTime);
                            }
                            return '';
                        }
                    },
                    {title: "操作", templet: '#auth-state',width: 350, align: 'center',}
                ]
            ]
        });


        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确认删除该API吗？', function (index) {
                    $.ajax({
                        url: '/apidetail/deleteOne',
                        data: {"apiId": data.apidetailId},
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            if (data.code == 0) {
                                layer.msg(data.msg, {
                                    offset: '15px',
                                    icon: 6,
                                    time: 1000
                                }, function () {
                                    table.reload('munu-table', {
                                    });
                                });
                            } else {
                                layer.msg(data.msg, {
                                    icon: 5,
                                    time: 2000
                                });
                            }
                        }, error: function (data) {
                            layer.msg("操作失败", {time: 1000, icon: 5});
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                window.location.href = '/apidetail/toApiAdd?apiId='+data.apidetailId;
            } else if (layEvent === 'export'){
                window.location.href = 'apidetail/export?apiId='+data.apidetailId;
            }
        });

        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });


</script>

</body>
</html>